package com.farm.wcp.util;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.farm.parameter.FarmParameterService;
import com.farm.util.cache.FarmCacheName;
import com.farm.util.cache.FarmCaches;

/**
 * 缓存和获取页面地址状态
 * 
 * @author macpl
 *
 */
public class Urlcaches {

	/**
	 * 获取页面状态记录的URL参数
	 * 
	 * @param request
	 * @return
	 */
	public static String getUrlParas(HttpServletRequest request) {
		String uuid = UUID.randomUUID().toString().replace("-", "");
		String url = null;
		String rurl = request.getHeader("Referer");
		if (rurl != null) {
			if (!FarmParameterService.getInstance().getParameter("config.wuc.sso.back.white.url").equals("NONE")) {
				List<String> whiteKey = FarmParameterService.getInstance()
						.getParameterStringList("config.wuc.sso.back.white.url");
				for (String key : whiteKey) {
					if (rurl.indexOf(key) >= 0) {
						url = rurl;
						break;
					}
				}
			} else {
				url = rurl;
			}
			FarmCaches.getInstance().putCacheData(uuid, url, FarmCacheName.wcpUrl);
		}
		return "&statecode=" + uuid;
	}

	/**
	 * 获取一个缓存的url
	 * 
	 * @param statecode
	 * @return
	 */
	public static String getUrl(String statecode) {
		String url = (String) FarmCaches.getInstance().getCacheData(statecode, FarmCacheName.wcpUrl);
		if (StringUtils.isNotBlank(url) && url.indexOf("STATE=") <= 0) {
			return url;
		}
		return null;
	}

}
